package com.ht.modules.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ht.modules.device.entity.DateUtils;
import com.ht.modules.device.entity.DeviceWaterDay;
import com.ht.modules.device.entity.DeviceWaterDayVo;
import com.ht.modules.device.mapper.DeviceWaterDayMapper;
import com.ht.modules.device.service.IDeviceWaterDayService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;

/**
 *  水表设备天数据
 * @Author:
 * @Date: 2025-03-25
 * @Version: V1.0
 */
@Service
public class DeviceWaterDayServiceImpl extends ServiceImpl<DeviceWaterDayMapper, DeviceWaterDay> implements IDeviceWaterDayService {

    @Autowired
    private DeviceWaterDayMapper deviceWaterDayMapper;

    /**
     * 分页列表查询
     *
     * @param deviceWaterDayVo 参数
     * @param page             分页对象
     * @param column           默认排序列
     * @param order            默认排序方式
     * @return 分页列表
     */
    @Override
    public Page<DeviceWaterDayVo> list(Page<DeviceWaterDayVo> page, DeviceWaterDayVo deviceWaterDayVo, String column, String order) {
        return page.setRecords(deviceWaterDayMapper.list(page, deviceWaterDayVo, column, order));
    }

    /**
     * 不分页列表查询
     *
     * @param deviceWaterDayVo 参数
     * @param column           默认排序列
     * @param order            默认排序方式
     * @return 不分页列表
     */
    @Override
    public List<DeviceWaterDayVo> listNoPage(DeviceWaterDayVo deviceWaterDayVo, String column, String order) {
        return deviceWaterDayMapper.list(null, deviceWaterDayVo, column, order);
    }

    /**
     * 添加
     *
     * @param deviceWaterDay 参数
     */
    @Override
    public void add(DeviceWaterDay deviceWaterDay) {
        // 校验日期
        if (null == deviceWaterDay.getDateTime()) {
            throw new RuntimeException("日期不能为空！");
        }
        // 校验数值
        if (null == deviceWaterDay.getValue()) {
            throw new RuntimeException("数值不能为空！");
        }
        // 校验日期是否已有数据
        long count = this.count(new LambdaQueryWrapper<DeviceWaterDay>()
                .eq(DeviceWaterDay::getDateTime, DateUtils.date2Str(deviceWaterDay.getDateTime(), new SimpleDateFormat("yyyy-MM-dd")))
        );
        if (count > 0) {
            throw new RuntimeException("日期已存在数据！");
        }
        this.save(deviceWaterDay);
    }

    /**
     * 编辑
     *
     * @param deviceWaterDay 参数
     */
    @Override
    public void edit(DeviceWaterDay deviceWaterDay) {
        DeviceWaterDay deviceWaterDayEntity = this.getById(deviceWaterDay.getId());
        if (null == deviceWaterDayEntity) {
            throw new RuntimeException("未找到对应实体！");
        }
        // 校验日期
        if (null == deviceWaterDay.getDateTime()) {
            throw new RuntimeException("日期不能为空！");
        }
        // 校验数值
        if (null == deviceWaterDay.getValue()) {
            throw new RuntimeException("数值不能为空！");
        }
        // 校验日期是否已有数据
        long count = this.count(new LambdaQueryWrapper<DeviceWaterDay>()
                .eq(DeviceWaterDay::getDateTime, DateUtils.date2Str(deviceWaterDay.getDateTime(), new SimpleDateFormat("yyyy-MM-dd")))
                .ne(DeviceWaterDay::getId, deviceWaterDay.getId())
        );
        if (count > 0) {
            throw new RuntimeException("日期已存在数据！");
        }
        this.updateById(deviceWaterDay);
    }

    /**
     * 通过id删除（逻辑删除）
     *
     * @param id 参数
     */
    @Override
    public void delete(Integer id) {
        DeviceWaterDay deviceWaterDay = this.getById(id);
        if (null == deviceWaterDay) {
            throw new RuntimeException("未找到对应实体！");
        }
        this.removeById(id);
    }

    /**
     * 批量删除（逻辑删除）
     *
     * @param ids 参数
     */
    @Override
    public void deleteBatch(String ids) {
        if (Strings.isBlank(ids)) {
            throw new RuntimeException("参数不识别！");
        }
        deviceWaterDayMapper.deleteBatchIds(Arrays.asList(ids.split(",")));
    }

    /**
     * 通过id查询
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public DeviceWaterDay queryById(Integer id) {
        DeviceWaterDay deviceWaterDay = this.getById(id);
        if (null == deviceWaterDay) {
            throw new RuntimeException("未找到对应实体！");
        }
        return deviceWaterDay;
    }

    /**
     * 通过id查询Vo
     *
     * @param id 参数
     * @return 查询结果
     */
    @Override
    public DeviceWaterDayVo queryVoById(Integer id) {
        DeviceWaterDayVo deviceWaterDayVo = deviceWaterDayMapper.getVoById(id);
        if (null == deviceWaterDayVo) {
            throw new RuntimeException("未找到对应实体！");
        }
        return deviceWaterDayVo;
    }

}
